package com.hit.aircraftwar.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.hit.aircraftwar.aircraft.HeroAircraft;
import com.hit.aircraftwar.application.GameView;
import com.hit.aircraftwar.application.ImageManager;
import com.hit.aircraftwar.httputil.MyWsManager;
import com.hit.aircraftwar.setter.ParameterSet;

import java.util.Objects;

public class GameActivity extends AppCompatActivity {
    private  static Handler mhandler;
    private int score;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Objects.requireNonNull(getSupportActionBar()).hide();
        mhandler = new Mhandler();

    }

    @Override
    protected void onResume() {
        super.onResume();
        GameView gameView = new GameView(this, mhandler);
        setContentView(gameView);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        HeroAircraft.getInstance().setLocation((int)event.getRawX(),(int)(event.getRawY() - ImageManager.HERO_BITMAP.getHeight()/2));
        return true;
    }

    public GameActivity() {
    }

    class Mhandler extends Handler {
        //进行UI的更新操作
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    //弹出结束的弹窗
                    ParameterSet.INSTANCE.updatePoint(msg.arg1);
                    ParameterSet.INSTANCE.resetMall();
                    if(ParameterSet.INSTANCE.isOnline) {
                        ParameterSet.INSTANCE.isOnline = false;
                        MyWsManager.getInstance().disconnect();
                    }
                    showDialog();
                    break;
            }
        }
    }


    //弹出消息框
    public void showDialog(){
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(this);
        normalDialog.setTitle("游戏结束");

        int score = GameView.getScore();
        int getPoint = (score/20) * ParameterSet.INSTANCE.getDifficulty();
        if(ParameterSet.INSTANCE.pointDouble) {
            getPoint *= 2;
        }
        normalDialog.setMessage("你的分数是:  "+ score + "\n" +
                "获得积分：" + getPoint);

        normalDialog.setPositiveButton("确定",
                (dialog, which) -> {
                    Intent intent = new Intent(GameActivity.this, RankingListActivity.class);
                    startActivity(intent);
                });
        // 显示
        normalDialog.show();
    }
}
